package com.we.connect.weconnect.quarte;

import com.we.connect.weconnect.model.remind.vo.RemindInfoVo;
import com.we.connect.weconnect.model.system.SysInfo;
import com.we.connect.weconnect.service.RemindInfoService;
import com.we.connect.weconnect.service.SysInfoService;
import com.we.connect.weconnect.utils.ScheduleUtils;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class QuartzInit {

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private RemindInfoService remindInfoService;

    @Autowired
    private SysInfoService sysInfoService;

    /**
     * 初始化系统时，把所有需要启动的任务全部启动
     */
    @PostConstruct
    public void init() throws Exception {
        scheduler.clear();
        // 查询所有的任务
        List<RemindInfoVo> allRemindInfoVoList = remindInfoService.getALLRemindInfoVoList();
        for(RemindInfoVo remindInfoVo : allRemindInfoVoList){
            SysInfo sysInfo = sysInfoService.findSysInfo(remindInfoVo.getUserId());
            ScheduleUtils.createScheduleJob(scheduler, remindInfoVo, sysInfo);
        }

    }
}
